from django.contrib import admin
from django.contrib.admin import ModelAdmin

from celery_tasks.html.tasks import generate_static_list_search_html
from goods import models


class SKUAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        # 修改或新增了商品，需要重新生成详情页
        obj.save()  # obj  商品sku对象
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html(obj.id)
        # generate_static_sku_detail_html.delay(obj.id)


class SKUSpecificationAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        """商品选项信息的修改"""
        obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html(obj.sku.id)
        # generate_static_sku_detail_html.delay(obj.sku.id)

    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html(sku_id)


class SKUImageAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html(obj.sku.id)

        # 判断商品是否有默认图片，如果没有， 则在新增商品图片时进行设置
        sku = obj.sku
        if not sku.default_image_url:  # 默认图片空
            sku.default_image_url = obj.image.url  # 设置商品默认图片
            sku.save()

    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html(sku_id)


class GoodsCategoryAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.tasks import generate_static_list_search_html
        # 重新生成商品列表页， 但不生成详情页
        generate_static_list_search_html.delay()

    def delete_model(self, request, obj):
        obj.delete()
        # 重新生成商品列表页， 但不生成详情页
        from celery_tasks.html.tasks import generate_static_list_search_html
        generate_static_list_search_html.delay()


admin.site.register(models.GoodsCategory, GoodsCategoryAdmin)
admin.site.register(models.GoodsChannel)
admin.site.register(models.Goods)
admin.site.register(models.Brand)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU, SKUAdmin)
admin.site.register(models.SKUSpecification, SKUSpecificationAdmin)
admin.site.register(models.SKUImage, SKUImageAdmin)






"""
class GoodsCategoryAdmin(ModelAdmin):
    # ctrl+o 重写modeladmin类里面的方法
    # 重写这两个方法的作用：只要在后台管理修改了数据就会调用
    # save_model和delete_model这两个方法，做修改前或后的额外操作　这里是修改后的操作

    # 保存新增或修改模型类，表字段
    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        # 重新生成静态的list.html页面  调用生成商品类别列表页面的方法
        generate_static_list_search_html.delay()  # 通过celery异步来生成静态首页桑品列表

    # 在管理后台删除一条类别数据　执行次方法
    def delete_model(self, request, obj):
        super().delete_model(request, obj)
        # 重新生成静态的list.html页面  调用生成商品类别列表页面的方法
        generate_static_list_search_html.delay()

# 注册模型  这里注册的是admin站点显示的模型表与字段　　　
admin.site.register(models.GoodsCategory,GoodsCategoryAdmin) # 商品类别
admin.site.register(models.GoodsChannel)
admin.site.register(models.Brand)
admin.site.register(models.Goods)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU)
admin.site.register(models.SKUImage)
admin.site.register(models.SKUSpecification)
"""


